Irregular Lattices

Dependencies

source("utils.R")
theme_set(theme_minimal())

For this representation of cells, we will rely on the SpatialFeatureExperiment package. For preprocessing of the dataset, we refer the reader to the vignette of the voyager package.

(sfe <- HeNSCLCData())
class: SpatialFeatureExperiment 
dim: 980 100290 
metadata(0):
assays(1): counts
rownames(980): AATK ABL1 ... NegPrb22 NegPrb23
rowData names(3): means vars cv2
colnames(100290): 1_1 1_2 ... 30_4759 30_4760
colData names(17): Area AspectRatio ... nCounts nGenes
reducedDimNames(0):
mainExpName: NULL
altExpNames(0):
spatialCoords names(2) : CenterX_global_px CenterY_global_px
imgData names(1): sample_id

unit: full_res_image_pixels
Geometries:
colGeometries: centroids (POINT), cellSeg (POLYGON) 

Graphs:
sample01: 
# Empty cells
colData(sfe)$is_empty <- colData(sfe)$nCounts < 1
# Select, count negative control probes
(neg_inds <- str_detect(rownames(sfe), "^NegPrb")) %>% sum
[1] 20
colData(sfe)$prop_neg <- colSums(counts(sfe)[neg_inds,])/colData(sfe)$nCounts
# Remove low quality cells
sfe <- sfe[,!sfe$is_empty & sfe$prop_neg < 0.1]
# Calculate count stats
rowData(sfe)$means <- rowMeans(counts(sfe))
rowData(sfe)$vars <- rowVars(counts(sfe))
rowData(sfe)$is_neg <- neg_inds
# log Counts
sfe <- logNormCounts(sfe)

In this vignette, we will show the metrics related a ligand-receptor pair, CEACAM6 and EGFR which was identified in the original publication of the CosMx dataset (heHighplexImagingRNA2022?).

plotSpatialFeature(sfe, c("CEACAM6"),
                   colGeometryName = "centroids", 
                   ncol = 2, scattermore = TRUE) + 
  theme_void()

plotSpatialFeature(sfe, c("EGFR"),
                   colGeometryName = "centroids", 
                   ncol = 2, scattermore = TRUE) + 
  theme_void()

colGraph(sfe, "knn5") <- findSpatialNeighbors(sfe, method = "knearneigh",
                                                  dist_type = "idw", k = 5, 
                                                  style = "W")
weights_neighbourhoods <- colGraph(sfe, "knn5")

Global Measures for Bivariate Data

Global Bivariate Moran’s I

For two continous observation the global bivariate Moran’s I is defined as (wartenbergMultivariateSpatialCorrelation1985? ; bivandPackagesAnalyzingSpatial2022?)

\[I_B = \frac{\Sigma_i(\Sigma_j{w_{ij}y_j\times x_i})}{\Sigma_i{x_i^2}}\]

where \(x_i\) and \(y_i\) are the two variables of interest and \(w_{ij}\) is the value of the spatial weights matrix for positions \(i\) and \(j\).

The global bivariate Moran’s I is a measure of autocorrelation of the variables \(x\) and \(y\) with the spatial lag of \(y\). Therefore the result might overestimate the spatial autocorrelation of the variables due to the inherent (non-spatial) correlation of \(x\) and \(y\) (bivandPackagesAnalyzingSpatial2022?).

Implementation using spded

res_xy <- spdep::moran_bv(x = logcounts(sfe)["CEACAM6",],
         y = logcounts(sfe)["EGFR",],
         listw =  colGraph(sfe, "knn5"),
         nsim = 499)
boot::boot.ci(res_xy, conf = c(0.99, 0.95, 0.9), type = "basic")
BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 499 bootstrap replicates

CALL : 
boot::boot.ci(boot.out = res_xy, conf = c(0.99, 0.95, 0.9), type = "basic")

Intervals : 
Level      Basic         
99%   ( 0.1284,  0.1361 )   
95%   ( 0.1297,  0.1355 )   
90%   ( 0.1300,  0.1349 )  
Calculations and Intervals on Original Scale
Some basic intervals may be unstable
plot(res_xy)

From the result of the global measure, the overall spatial autocorrelation of the two genes is not significant.

Global Bivariate Lee’s L

Lee’s L is a bivariate measure that combines non-spatial pearson correlation with spatial autocorrelation via Moran’s I (leeDevelopingBivariateSpatial2001?). This enables us to asses the spatial dependence of two continuous variables in a single measure. The measure is defined as

\[L(x,y) = \frac{n}{\sum_{i=1}^n(\sum_{j=1}^nw_{ij})^2}\frac{\sum_{i=1}^n(\sum_{j=1}^nw_{ij}(x_i-\bar{x}))(\sum_{j=1}^nw_{ij}(y_j-\bar{y}))}{\sqrt{\sum_{i=1}^nw_{ij}(x_i-\bar{x})^2}\sqrt{\sum_{i=1}^nw_{ij}(y_i-\bar{y})^2}}, \]

where \(w_{ij}\) is the value of the spatial weights matrix for positions \(i\) and \(j\), \(x\) and \(y\) the two variables and \(\bar{x}\) and \(\bar{y}\) their means.

Implementation using spded

res_lee <- calculateBivariate(sfe, type = "lee.mc", 
                   feature1 = "CEACAM6", feature2 = "EGFR",
                   nsim = 499)
res_lee$lee.mc_statistic
 statistic 
0.06727921 
res_lee$ lee.mc_p.value
[1] 0.002

The interpretation of the result is not straightforward, as identical patterns do not have a value of 1 but have a value depending on their spatial arrangement. In general positive values indicate spatial co-occurrence while negative values indicate spatial segregation of the pattern of variables of \(x\) and \(y\) (leeDevelopingBivariateSpatial2001?).

Local Measures for Bivariate Data

Bivariate Lee’s L

Similar to the global variant of Lee’s L the local variant (leeDevelopingBivariateSpatial2001?) is defined as

\[L_i(x,y) = \frac{\sum_{i=1}^n(\sum_{j=1}^nw_{ij}(x_i-\bar{x}))(\sum_{j=1}^nw_{ij}(y_j-\bar{y}))}{\sqrt{\sum_{i=1}^nw_{ij}(x_i-\bar{x})^2}\sqrt{\sum_{i=1}^nw_{ij}(y_i-\bar{y})^2}}, \] Local Lee’s L is a measure of spatial co-expression, when the variables of interest are gene expression measurements and can also be a metric of co-localization. Unlike the gobal version, the variables are not averaged and show the local contribution to the metric. Positive values indicate colocalization, negative values indicate segregation.

This can be interesting in the context of detection of coexpressed ligand-receptor pairs. A method that is based on bivariate Moran’s I and tries to detect such pairs is SpatialDM (liSpatialDMRapidIdentification2023?).

Implementation using voyager

sfe <- runBivariate(sfe, type = "locallee",
                    feature1 = "CEACAM6", feature2 = "EGFR")

plotLocalResult(sfe, "locallee", 
                features = localResultFeatures(sfe, "locallee"),
                ncol = 2, colGeometryName = "centroids",
                divergent = TRUE, diverge_center = 0) 

Local Measures for Multivariate Data

Multivariate local Geary’s C

Geary’s C is a measure of spatial autocorrelation that is based on the difference between a variable and its neighbours. (anselinLocalIndicatorMultivariate2019?) defines it as

\[C_i = \sum_{j=1}^n w_{ij}(z_i-z_j)^2,\]

and can be generalized to \(k\) parameters by expanding

\[c_{k,i} = \sum_{v=1}^k c_{v,i}\]

where \(c_{v,i}\) is the local Geary’s C for the \(v\)th variable at location \(i\). The number of variables that can be used is not fixed, which makes the interpretation a bit more difficult. In general, the metric summarizes similarity in the “multivariate attribute space” (i.e. the gene expression) to its geographic neighbours. The common difficulty in these analyses is the interpretaing the mixture of similarity in the geographic space and similarity in the attribute space.

Here we use the marker genes used in the original paper that study apoptosis (heHighplexImagingRNA2022?, Supplementary Table 1) in a subset of the tissue, as the computation is very intensive.

apoptosis_genes <- c("BAX", "BCL2", "BCL2L1", "BIRC5", "CASP3", "CASP8", "TP53")

# Subset of the tissue
bbox_use <- st_as_sfc(st_bbox(c(xmin = 3200, xmax = 15800, ymin = 155200, ymax = 164200)))
sfe_sub <- sfe[,st_intersects(colGeometries(sfe)$centroids, bbox_use, sparse = FALSE)]

sfe_sub <- runMultivariate(sfe_sub, type = "localC_multi",
                    subset_row = apoptosis_genes)

# Local C mutli is stored in colData so this is a workaround to plot it
plotSpatialFeature(sfe_sub, "localC_multi", colGeometryName = "centroids")

The plot indicates regions where the gene expression is more homogeneous (low Geary’s C) and regions where the gene expression is more heterogeneous (large Geary’s C value). Importantly, strong similarity in one or some variables may compensate for dissimilarity in other variables, which makes the interpretation tricky. The local Geary’s C value is not scaled.

We can further plot the results of the permutation test. Significant values indicate interesting regions, but should be interpreted with care for various reasons. For example, we are looking for similarity in a combination of multiple values but the exact combination is not known. For further details, see (anselinLocalIndicatorMultivariate2019?).

(MR: this makes me question how we should present this. SG: Good question, it comes very close to spatial clustering here, maybe we can show this as a preview for other methods that we do not discuss.. From the anselinLocalIndicatorMultivariate2019? publication: “Overall, however, the statistic indicates a combination of the notion of distance in multi-attribute space with that of geographic neighbors. This is the essence of any spatial autocorrelation statistic. It is also the trade-off encountered in spatially constrained multivariate clustering methods (for a recent discussion, see, e.g., Grubesic, Wei, and Murray 2014).”)

sfe_sub <- runMultivariate(sfe_sub, type = "localC_perm_multi",
                    subset_row = apoptosis_genes,
                    nsim = 100)

# stored as spatially reduced dim; plot it in this way
spatialReducedDim(sfe_sub, "localC_perm_multi",  c(1, 11))

It would be interesting to do this not only on the cell level, but also on the domain level.

Local Neighbor Match Test

This test is useful to assess the overlap of the k-nearest neighbours from physical distances (tissue space) with the k-nearest neighbours from the gene expression measurements (attribute space).

[MR: how are the KNNs calculated in the attribute space?]

sf <- colGeometries(sfe)$cellSeg
sf <- cbind(sf,  t(as.matrix(logcounts(sfe)[c(apoptosis_genes),])))

nbr_test <- neighbor_match_test(sf[c(apoptosis_genes)], k = 20)

sf$Probability <- nbr_test$Probability
sf$Cardinality <- nbr_test$Cardinality

tm_shape(sf) + tm_fill(col = 'Probability')  

tm_shape(sf) + tm_fill(col = 'Cardinality')  

Cardinality is a measure of how many neighbours of each cell are in common. Some regions show high cardinality with low probability. On the cellular level, this might not be very informative and lead to regions with high similarity. We only see very few cells with a cardinality greater than 0. The problem might come from the imperfect segmentation of the cells, which can result in a poor reconstruction of the “geographical” tissue neighbourhood graph. In addition, as the number of dimensions increase, the empty space between observations also increases; this is known as the empty space problem.

Again, this analysis should probably be performed on a structure level instead of a single cell level.

[MR: should we do the analysis at the structure/domain level then?]

Appendix

Session info

sessionInfo()
R version 4.3.1 (2023-06-16)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Monterey 12.6.1

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: Europe/Zurich
tzcode source: internal

attached base packages:
[1] stats4    stats     graphics  grDevices utils     datasets  methods  
[8] base     

other attached packages:
 [1] stringr_1.5.1                  dixon_0.0-9                   
 [3] splancs_2.01-44                spdep_1.3-1                   
 [5] spData_2.3.0                   tmap_3.3-4                    
 [7] scater_1.29.4                  scran_1.28.2                  
 [9] scuttle_1.10.3                 SFEData_1.2.0                 
[11] SpatialFeatureExperiment_1.2.3 Voyager_1.2.7                 
[13] rgeoda_0.0.10-4                digest_0.6.34                 
[15] ncf_1.3-2                      sf_1.0-15                     
[17] reshape2_1.4.4                 patchwork_1.2.0               
[19] STexampleData_1.8.0            ExperimentHub_2.8.1           
[21] AnnotationHub_3.8.0            BiocFileCache_2.11.1          
[23] dbplyr_2.4.0                   RANN_2.6.1                    
[25] seg_0.5-7                      sp_2.1-2                      
[27] rlang_1.1.3                    ggplot2_3.4.4                 
[29] dplyr_1.1.4                    mixR_0.2.0                    
[31] spatstat_3.0-7                 spatstat.linnet_3.1-3         
[33] spatstat.model_3.2-8           rpart_4.1.19                  
[35] spatstat.explore_3.2-5         nlme_3.1-162                  
[37] spatstat.random_3.2-2          spatstat.geom_3.2-7           
[39] spatstat.data_3.0-4            SpatialExperiment_1.10.0      
[41] SingleCellExperiment_1.22.0    SummarizedExperiment_1.30.2   
[43] Biobase_2.60.0                 GenomicRanges_1.52.1          
[45] GenomeInfoDb_1.36.4            IRanges_2.34.1                
[47] S4Vectors_0.38.2               BiocGenerics_0.46.0           
[49] MatrixGenerics_1.12.3          matrixStats_1.2.0             

loaded via a namespace (and not attached):
  [1] spatstat.sparse_3.0-3         bitops_1.0-7                 
  [3] httr_1.4.7                    RColorBrewer_1.1-3           
  [5] tools_4.3.1                   utf8_1.2.4                   
  [7] R6_2.5.1                      HDF5Array_1.28.1             
  [9] mgcv_1.8-42                   rhdf5filters_1.12.1          
 [11] withr_3.0.0                   gridExtra_2.3                
 [13] leaflet_2.2.1                 leafem_0.2.3                 
 [15] cli_3.6.2                     labeling_0.4.3               
 [17] proxy_0.4-27                  R.utils_2.12.3               
 [19] dichromat_2.0-0.1             scico_1.5.0                  
 [21] limma_3.56.2                  rstudioapi_0.15.0            
 [23] RSQLite_2.3.4                 generics_0.1.3               
 [25] crosstalk_1.2.1               Matrix_1.6-5                 
 [27] ggbeeswarm_0.7.2              fansi_1.0.6                  
 [29] abind_1.4-5                   R.methodsS3_1.8.2            
 [31] terra_1.7-65                  lifecycle_1.0.4              
 [33] yaml_2.3.8                    edgeR_3.42.4                 
 [35] rhdf5_2.44.0                  tmaptools_3.1-1              
 [37] grid_4.3.1                    blob_1.2.4                   
 [39] promises_1.2.1                dqrng_0.3.2                  
 [41] crayon_1.5.2                  lattice_0.21-8               
 [43] beachmat_2.16.0               KEGGREST_1.40.1              
 [45] magick_2.8.2                  pillar_1.9.0                 
 [47] knitr_1.45                    metapod_1.7.0                
 [49] rjson_0.2.21                  boot_1.3-28.1                
 [51] codetools_0.2-19              wk_0.9.1                     
 [53] glue_1.7.0                    vctrs_0.6.5                  
 [55] png_0.1-8                     gtable_0.3.4                 
 [57] cachem_1.0.8                  xfun_0.41                    
 [59] S4Arrays_1.0.6                mime_0.12                    
 [61] DropletUtils_1.20.0           units_0.8-5                  
 [63] statmod_1.5.0                 bluster_1.10.0               
 [65] interactiveDisplayBase_1.38.0 ellipsis_0.3.2               
 [67] bit64_4.0.5                   filelock_1.0.3               
 [69] irlba_2.3.5.1                 vipor_0.4.7                  
 [71] KernSmooth_2.23-21            colorspace_2.1-0             
 [73] DBI_1.2.1                     raster_3.6-26                
 [75] tidyselect_1.2.0              bit_4.0.5                    
 [77] compiler_4.3.1                curl_5.2.0                   
 [79] BiocNeighbors_1.18.0          DelayedArray_0.26.7          
 [81] scales_1.3.0                  classInt_0.4-10              
 [83] rappdirs_0.3.3                goftest_1.2-3                
 [85] spatstat.utils_3.0-4          rmarkdown_2.25               
 [87] XVector_0.40.0                htmltools_0.5.7              
 [89] pkgconfig_2.0.3               base64enc_0.1-3              
 [91] sparseMatrixStats_1.12.2      fastmap_1.1.1                
 [93] htmlwidgets_1.6.4             shiny_1.8.0                  
 [95] DelayedMatrixStats_1.22.6     farver_2.1.1                 
 [97] jsonlite_1.8.8                BiocParallel_1.34.2          
 [99] R.oo_1.25.0                   BiocSingular_1.16.0          
[101] RCurl_1.98-1.14               magrittr_2.0.3               
[103] GenomeInfoDbData_1.2.10       s2_1.1.6                     
[105] Rhdf5lib_1.22.1               munsell_0.5.0                
[107] Rcpp_1.0.12                   ggnewscale_0.4.9             
[109] viridis_0.6.4                 stringi_1.8.3                
[111] leafsync_0.1.0                zlibbioc_1.46.0              
[113] plyr_1.8.9                    parallel_4.3.1               
[115] ggrepel_0.9.5                 deldir_2.0-2                 
[117] Biostrings_2.68.1             stars_0.6-4                  
[119] splines_4.3.1                 tensor_1.5                   
[121] locfit_1.5-9.8                igraph_1.6.0                 
[123] ScaledMatrix_1.8.1            BiocVersion_3.17.1           
[125] XML_3.99-0.16                 evaluate_0.23                
[127] BiocManager_1.30.22.3         httpuv_1.6.13                
[129] purrr_1.0.2                   polyclip_1.10-6              
[131] scattermore_1.2               rsvd_1.0.5                   
[133] lwgeom_0.2-13                 xtable_1.8-4                 
[135] e1071_1.7-14                  RSpectra_0.16-1              
[137] later_1.3.2                   viridisLite_0.4.2            
[139] class_7.3-22                  tibble_3.2.1                 
[141] memoise_2.0.1                 beeswarm_0.4.0               
[143] AnnotationDbi_1.62.2          cluster_2.1.4